//
//  BASiteListCollectionViewCell.swift
//  BadAir
//
//  Created by D on 2018/3/7.
//  Copyright © 2018年 D. All rights reserved.
//

import UIKit
import SnapKit

class BASiteListCell: UITableViewCell {
    
    var model: BAAQICityModel? {
        didSet{
            updateData()
        }
    }
    
    private let nameLabel = UILabel()
    private let aqiLabel = UILabel()
    private let aqiValueLabel = UILabel()
    
    private let pm25View = BAElementView()
    private let pm10View = BAElementView()
    private let pm10SubView = BAElementView()
    private let O3View = BAElementView()
    private let NO2View = BAElementView()
    private let COView = BAElementView()
    private let SO2View = BAElementView()
    
    private func updateData() {
        nameLabel.text = model?.site_name == "" ? model?.area : model?.site_name
        if (model?.aqi == "_") {
            aqiValueLabel.text = "-"
        }else {
            aqiValueLabel.text = model?.aqi
        }
        
        
        pm25View.value = model?.pm2_5
        pm10View.value = model?.pm10
        pm10SubView.value = model?.pm10
        NO2View.value = model?.no2
        SO2View.value = model?.so2
        COView.value = model?.co
        O3View.value = model?.o3
    }
    
    private func setupUI() {
        backgroundColor = UIColor.clear
        selectionStyle = .none
        
        let shadowImg = #imageLiteral(resourceName: "img_shadow").resizableImage(withCapInsets: UIEdgeInsets(top: 50,left: 50,bottom: 50,right: 50), resizingMode: UIImageResizingMode.stretch)
        let shadowView = UIImageView(image: shadowImg)
        addSubview(shadowView)
        let backView = UIView()
        backView.backgroundColor = colorWhite
        backView.layer.cornerRadius = 15
        backView.layer.masksToBounds = true
        addSubview(backView)
        
        
        nameLabel.setupLabel(ofSize: 22, weight: .semibold, txtColor: UIColor("#1D2D47"))
        addSubview(nameLabel)
        aqiValueLabel.setupLabel(ofSize: 32, weight: .light, txtColor: UIColor("#1D2D47"))
        addSubview(aqiValueLabel)
        let aqiTextLabel = UILabel()
        aqiTextLabel.setupLabel(ofSize: 18, weight: .light, txtColor: UIColor("#7D8CA0"))
        aqiTextLabel.text = "AQI"
        addSubview(aqiTextLabel)
        
        // 元素信息
        pm25View.titleType = .PM25
        pm10View.titleType = .PM10
        NO2View.titleType = .NO2
        SO2View.titleType = .SO2
        COView.titleType = .CO
        O3View.titleType = .O3
        addSubview(pm25View)
        addSubview(pm10View)
        addSubview(NO2View)
        addSubview(SO2View)
        addSubview(COView)
        addSubview(O3View)
        pm10View.alpha = 0
        COView.alpha = 0
        O3View.alpha = 0
        
        // 约束
        backView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.bottom.right.equalTo(-15)
            make.left.equalTo(15)
        }
        shadowView.snp.makeConstraints { (make) in
            make.height.equalTo(100)
            make.left.right.equalTo(backView)
            make.bottom.equalToSuperview()
        }
        
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(backView).offset(20)
            make.top.equalTo(backView).offset(24)
        }
        aqiValueLabel.snp.makeConstraints { (make) in
            make.right.equalTo(backView).offset(-38)
            make.top.equalTo(backView).offset(24)
        }
        aqiTextLabel.snp.makeConstraints { (make) in
            make.top.equalTo(aqiValueLabel.snp.bottom).offset(0)
            make.right.equalTo(aqiValueLabel)
        }
        
        let eleSpace = 22
        
        pm25View.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(11)
            make.left.equalTo(nameLabel)
//            make.width.equalTo(eleWidth)
//            make.height.equalTo(eleHeight)
        }
        NO2View.snp.makeConstraints { (make) in
            make.top.equalTo(pm25View)
            make.left.equalTo(pm25View.snp.right).offset(eleSpace)
//            make.width.height.equalTo(pm25View)
        }
        SO2View.snp.makeConstraints { (make) in
            make.top.equalTo(pm25View)
            make.left.equalTo(NO2View.snp.right).offset(eleSpace)
//            make.width.height.equalTo(pm25View)
        }
        
        
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
